
package com.cuit.xsc.weishare.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.cuit.xsc.weishare.R;
import com.cuit.xsc.weishare.adapter.WeiboAdapter;
import com.cuit.xsc.weishare.logic.MainService;
import com.cuit.xsc.weishare.logic.Task;
import com.cuit.xsc.weishare.model.Statuse;
import com.cuit.xsc.weishare.model.UserInfo;
import com.cuit.xsc.weishare.util.ConfigUtil;
import com.cuit.xsc.weishare.util.LogUtil;
import com.cuit.xsc.weishare.widget.PullToRefreshListView;
import com.cuit.xsc.weishare.widget.PullToRefreshListView.OnRefreshListener;


public class HomeAct extends BaseActivity implements OnClickListener
{


	private static final String TAG="HomeActivity";
	private UserInfo nowUser;
	
	//加载View
	private View progresView;
	
	private View titleView;
	
	private Button editButton;
	
	private List<Statuse> list;
	
	WeiboAdapter adapter;
	
	private View footerView = null;
	
	private ProgressBar footPoProgressBar;
	
	
	private PullToRefreshListView weiboListView;
	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.home);
	    init();
	
		
	}


	/**
	 * 添加获取微博信息任务
	 */
	private void newTask()
	{
		Task task = new Task(Task.WEIBO_FRIEDNS_TIMELINE,null);
		
		MainService.addTask(task);
		
	//	MainService.addActivity(this);
	}


	public void init()
	{
		//nowUser = WeiShareApplication.getInstance().getCurrentUser();
		nowUser = MainService.getCurrentUser();
		
		weiboListView = (PullToRefreshListView) this.findViewById(R.id.lv_weibos);
		

		 LayoutInflater inflater = LayoutInflater.from(this);
		 footerView = inflater.inflate(R.layout.listview_foot, null);
		 footPoProgressBar = (ProgressBar) footerView.findViewById(R.id.listview_foot_refresh_progress);
		 footerView.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				//footPoProgressBar.setVisibility(View.VISIBLE);
				getOldHomeTimeline();
			}
		});

		weiboListView.addFooterView(footerView,null,true);
		
		weiboListView.setOnRefreshListener(new OnRefreshListener()
		{	
			@Override
			public void onRefresh()
			{
				getNewHomeTimeline();
			}
		});
		
		progresView = this.findViewById(R.id.layout_progress);		
		titleView = this.findViewById(R.id.layout_title_bar);
	   ((TextView)titleView.findViewById(R.id.txt_wb_title)).setText(nowUser.getUserName());	
	   editButton = (Button) findViewById(R.id.btn_writer);
	   editButton.setVisibility(View.VISIBLE);
	   editButton.setOnClickListener(this);
		
		newTask();
		
		LogUtil.i(this, "progresView="+progresView);
		LogUtil.i(this, "weiboListView="+weiboListView);
		
		
		
		
		
		
		
		
		
	}


	@SuppressWarnings("unchecked")
	public void refresh(Object... params)
	{
		progresView.setVisibility(View.GONE);
		
		int flag = ((Integer) params[0]).intValue();
		switch (flag)
		{
		case Task.WEIBO_FRIEDNS_TIMELINE:
			list = (List<Statuse>) params[1];
			if (list != null && !list.isEmpty())
			{
				LogUtil.i(this, "list size:"+list.size());
				
				for (Statuse statuse : list)
				{
					LogUtil.e(this, "wei id="+statuse.getId());
				}
				
				adapter = new WeiboAdapter(this);
				adapter.setList(list);
				adapter.setListView(weiboListView);
				weiboListView.setAdapter(adapter);
			}
			break;
		case Task.GET_NEW_HOME_TIMELINE:
			
			List<Statuse> newList = (List<Statuse>) params[1];			
			if (newList != null && !newList.isEmpty())
			{
				list.addAll(0, newList);
				LogUtil.i(this, "new list size:"+list.size());				
				adapter.setList(list);
				adapter.notifyDataSetChanged();
				weiboListView.onRefreshComplete();
			}
			else
			{
				weiboListView.onRefreshComplete();
				Toast.makeText(this, "没有新的微博更新", Toast.LENGTH_SHORT).show();
			}
			
			
			
			break;
			
		case Task.GET_OLD_HOME_TIMELINE:
			footPoProgressBar.setVisibility(View.GONE);
			List<Statuse> oldList = (List<Statuse>) params[1];			
			if (oldList != null && !oldList.isEmpty())
			{
				list.addAll(oldList);
				//LogUtil.i(this, "new list size:"+list.size());				
				adapter.setList(list);
				adapter.notifyDataSetChanged();
				//weiboListView.onRefreshComplete();
			}
			else
			{
				//weiboListView.onRefreshComplete();
				Toast.makeText(this, "没有旧的微博了", Toast.LENGTH_SHORT).show();
			}
			
			break;

		default:
			break;
		}
		
		
		
//		List<Status> status = (List<Status>) params[0];
//		
//		
//		WeiboAdapter adapter = new WeiboAdapter(this, status);
//		weiboListView.setAdapter(adapter);
		
		Log.i(TAG, "ok--------");
	}


	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_writer:
			Intent intent = new Intent(this, CreativeDesignAct.class);
			startActivity(intent);			
			break;
		default:
			break;
		}
		
	}
	
	
	private void getNewHomeTimeline()
	{
		
		if (list != null && !list.isEmpty())
		{
			Statuse statuse = list.get(0);
			String type = nowUser.getUserType();
			Map map = new HashMap();
			if (type.equals(ConfigUtil.QQW))
			{
				map.put("p", statuse.getTimestamp());
			}
			else if (type.equals(ConfigUtil.SINAW))
			{
				map.put("p", statuse.getId());
			}

			Task task = new Task(Task.GET_NEW_HOME_TIMELINE, map);
			MainService.addTask(task);
		}

	}
	
	
	private void getOldHomeTimeline()
	{
		
		if (footPoProgressBar.getVisibility() == View.VISIBLE)
		{
			return;
		}
		
		if (list != null && !list.isEmpty())
		{
			footPoProgressBar.setVisibility(View.VISIBLE);
			int size = list.size();
			Statuse statuse = list.get(size -1);
			String type = nowUser.getUserType();
			Map map = new HashMap();
			if (type.equals(ConfigUtil.QQW))
			{
				map.put("p", statuse.getTimestamp());
			}
			else if (type.equals(ConfigUtil.SINAW))
			{
				map.put("p", statuse.getId());
			}

			Task task = new Task(Task.GET_OLD_HOME_TIMELINE, map);
			MainService.addTask(task);
		}
		
	}
	
	
}
